
	  
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.Common.BLL;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Youz.CreditRepay.BLL;

namespace Youz.CreditRepay.Manage.Handlers
{
    public partial class CMS_MessageHandler : IHttpHandler, IRequiresSessionState
    {
		CMS_MessageBLL bll = new CMS_MessageBLL();
		
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
			
            string _type = WebHelper.Get("type");
            string result = string.Empty;
            switch (_type)
            {
                case "get":
                    result = Get();
                    break;  
                case "list":
                    result = List();
                    break;
                case "save":
                    result = Save();
                    break;
                case "delete":
                    result = Delete();
                    break;
            }
			
            context.Response.Write(result);
            context.Response.End();
        }
		
        private string Get()
        {
            Int64 MessageID = WebHelper.Post<Int64>("MessageID");
            
            return bll.GetJson(MessageID);
        }

        private string List()
        {
            int pageIndex = WebHelper.Post<int>("pageIndex");
            int pageSize = WebHelper.Post<int>("pageSize");


            StringBuilder sbsql = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(WebHelper.Post<string>("MemberAccount")))
                sbsql.AppendFormat("MemberAccount='{0}'", WebHelper.Post<string>("MemberAccount"));

            int total = 0;
            DataTable dt = bll.Gets(sbsql.ToString(), pageSize * (pageIndex - 1), pageSize, out total);
            return Core.GetPageByDataTableHaveTime(total, dt);
        }

        private string Save()
        {
            string id = WebHelper.Post("MessageID");
            bool isAdd = string.IsNullOrWhiteSpace(id);          
            CMS_Message model = new CMS_Message();            

            if (isAdd)
            {
            }
            else
            {
                model = bll.Get(WebHelper.Post<Int64>("MessageID"));
            }

            model.Title = WebHelper.Post<String>("Title");
			model.Content = WebHelper.Post<String>("Content");
			model.MemberAccount = WebHelper.Post<String>("MemberAccount");
			model.IsRead = WebHelper.Post<Boolean>("IsRead");
			model.Creator = WebHelper.Post<String>("Creator");
			model.CreateTime = WebHelper.Post<DateTime>("CreateTime");
			

            if (isAdd ? bll.Add(model) : bll.Update(model))
            {
                return Utility.AjaxResult(true, AjaxResultType.Save);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Save);
            }
        }
		
        private string Delete() 
        {
            if (bll.Delete(WebHelper.Post<Int64>("MessageID")))
            {
                return Utility.AjaxResult(true, AjaxResultType.Delete);
            }
            else
            {
                return Utility.AjaxResult(false, AjaxResultType.Delete);
            }
        }
		
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  